package com.sysu.simulation.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.sysu.simulation.R;

public class RecyclerSwipeRefreshLayout extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener{

    private RefreshLoadListener listener;
    private RecyclerView recyclerView;
    private int mScaledTouchSlop;
    private float startY;
    private float endY;
    private boolean isLoading = false;

    public RecyclerSwipeRefreshLayout(Context context) {
        super(context, null);
    }

    public RecyclerSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        if(listener != null && !isLoading) {
            setRefreshing(true);
            listener.refresh();
        }
        else setRefreshing(false);
    }

    public void onLoad() {
        if(listener != null && !isRefreshing()) {
            setLoading(true);
            listener.load();
        }
        else setLoading(false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (recyclerView == null) {
            if (getChildCount() > 0) {
                if (getChildAt(0) instanceof RecyclerView) {
                    recyclerView = (RecyclerView) getChildAt(0);
                }
                else {
                    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
                }
                if(recyclerView != null) setRecyclerScrollListener();
            }
        }
    }

    private void setRecyclerScrollListener() {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if(canLoadMore()) {
                    onLoad();
                }
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                endY = event.getRawY();
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    private boolean canLoadMore() {
        boolean isPullUp = (startY-endY)>=mScaledTouchSlop;
        boolean isBottom = false;
        if(recyclerView!=null && recyclerView.getAdapter()!=null) {
            isBottom = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastVisibleItemPosition()
                    == (recyclerView.getAdapter().getItemCount()-1);
        }
        return isPullUp && isBottom && !isLoading;
    }

    public void setLoading(boolean loading) {
        isLoading = loading;
        if(!loading) {
            startY = 0;
            endY = 0;
        }
    }

    public void complete() {
        setRefreshing(false);
        setLoading(false);
    }

    public void setRefreshLoadListener(RefreshLoadListener l) {
        listener = l;
    }

    public interface RefreshLoadListener {
        void refresh();
        void load();
    }
}
